
using System;
using Gtk;
using System.IO;

namespace wc_gui_input
{
	
	public class mod_categ : Gtk.Dialog
	{
		protected Gtk.Button but_add;
		protected Gtk.Entry entry_title;
		protected Gtk.Button but_delete;
		protected Gtk.Button but_edit;
		protected Gtk.ComboBox combo_location;
		protected Gtk.ComboBox combo_page;

		protected ListStore store_filenames;
		protected ListStore store_cat;
		protected ListStore store_cat2;	
		protected String cat_filename;
		protected String active_filename;
		protected String active_category;

		public mod_categ(String page_filename)
		{
			Stetic.Gui.Build(this, typeof(wc_gui_input.mod_categ));
			cat_filename=page_filename;
			// store holds values
        	store_filenames = new ListStore(typeof (string));
        	store_cat = new ListStore(typeof (string));
        	store_cat2 = new ListStore(typeof (string));
			
			init_widgets();
		}
		
		protected void init_widgets() {
			TextReader categ;
			try {// config file to read from
	      		categ = new StreamReader(cat_filename);  
	    	} catch(FileNotFoundException exc) { 
	      		Console.WriteLine(exc.Message); 
	      		return; 
	    	}
	    	// initialize for combo_box
	    	CellRendererText cell = new CellRendererText();
	        
	        // erase old data
	        store_cat.Clear();
	        store_cat2.Clear();
	        store_filenames.Clear();
	        
	        // initialize modify/delete existing category
	        combo_page.Clear();
	    	combo_page.PackStart(cell, false);
	        combo_page.AddAttribute(cell, "text", 0);
	        
			// initialize new category location
	        combo_location.Clear();
	    	combo_location.PackStart(cell, false);
	        combo_location.AddAttribute(cell, "text", 0);
	        
	        // assign stores
	        combo_page.Model = store_cat;
	        combo_location.Model = store_cat2;
	        
	        // populate stores into combo box
	        int i=0;
	    	while (categ.Peek() != -1) {
	    		String temp = categ.ReadLine();
				store_filenames.AppendValues(0,temp.IndexOf(' '));
	    		String temp2 = temp.Substring(temp.IndexOf(' ')+1);
	    		store_cat.AppendValues(temp2.Substring(0,temp2.Length));
	    		store_cat2.AppendValues("\n");
	    		store_cat2.AppendValues(temp2.Substring(0,temp2.Length));
	    		if (i==0) { // store for default filename to be sent for editing
	    			active_filename=temp.Substring(0,temp.IndexOf(' '));
	    			active_category=temp2.Substring(0,temp2.Length);
	    		}
				i++;
	    	}
	    	store_cat2.AppendValues("\n");
			categ.Close();
			
			// set combo boxes to have initial active item
			combo_page.Active = 0;
			combo_location.Active = 0;
			
			// clear textbox
			entry_title.DeleteText(0, entry_title.Text.Length);
		}

		protected virtual void OnButAddClicked(object sender, System.EventArgs e)
		{
			wc_gui_input.edit_page my_new_page = new wc_gui_input.edit_page("","");
		}

		protected virtual void OnButDeleteClicked(object sender, System.EventArgs e)
		{
			TextReader infile;// infile is page ( category --> page )
			try {
	 			infile = new StreamReader(cat_filename); // file to read from 
	    	} catch(FileNotFoundException exc) { 
	      		Console.WriteLine(exc.Message); 
	      		return; 
	    	}

			FileStream outfile; // file to write to
			try {
				outfile = new FileStream(cat_filename+".backup", FileMode.OpenOrCreate, FileAccess.Write);
			} catch(UnauthorizedAccessException exc) {
				Console.WriteLine(exc.Message);
				return;
			}
			
			// Create a new stream to write to the file
			StreamWriter sw = new StreamWriter(outfile);

	        int line_count = 0;
	        while (infile.Peek() != -1) // reading file to write in formatted html to file
			{
				String temp = infile.ReadLine();
				if (line_count != combo_page.Active) {
					// skip writing the category that user selected
					sw.WriteLine(temp);
					//Console.WriteLine(temp);
				}
				line_count++;// increase # to append next template text file
			}
			infile.Close();
			sw.Close();
			outfile.Close();

			File.Delete(cat_filename);
			File.Move(cat_filename+".backup",cat_filename);
			init_widgets();
		}

		protected virtual void OnButEditClicked(object sender, System.EventArgs e)
		{
			Console.WriteLine(active_filename+"\n"+ active_category);
			wc_gui_input.edit_page my_new_page = new wc_gui_input.edit_page(active_filename, active_category);
		}

		protected virtual void OnComboPageChanged(object sender, System.EventArgs e)
		{
			TextReader categ;
			try {// config file to read from
	      		categ = new StreamReader(cat_filename);  
	    	} catch(FileNotFoundException exc) { 
	      		Console.WriteLine(exc.Message); 
	      		return; 
	    	}
	        
	        int i=0;
	    	while (categ.Peek() != -1) {
	    		String temp = categ.ReadLine();
				String temp2 = temp.Substring(temp.IndexOf(' ')+1);
	    		if (i==combo_page.Active) { // store for default filename to be sent for editing
					active_filename=temp.Substring(0,temp.IndexOf(' '));
	    			active_category=temp2.Substring(0,temp2.Length);
	    		}
				i++;
	    	}
		}
	}
	
}
